package com.wm.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wm.commonutils.result.ResultCode;
import com.wm.eduService.entity.EduChapter;
import com.wm.eduService.entity.EduVideo;
import com.wm.eduService.entity.vo.ChapterVo;
import com.wm.eduService.entity.vo.VideoVo;
import com.wm.eduService.mapper.EduChapterMapper;
import com.wm.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.eduService.service.EduVideoService;
import com.wm.servicebase.exceptionhandle.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author sshdg
 * @since 2020-10-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    EduVideoService videoService;


    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id 查询课程里面所有的章节
        LambdaQueryWrapper<EduChapter> chapterQuery = new LambdaQueryWrapper<>();
        chapterQuery.eq(EduChapter::getCourseId,courseId);
        chapterQuery.orderByDesc(EduChapter::getSort);
        List<EduChapter> chapterList = this.list(chapterQuery);

        //封装小节
        List<ChapterVo> chapterVoList = chapterList.stream()
                .map(chapter -> new ChapterVo(chapter.getId(), chapter.getTitle()))
                .collect(toList());

        //2.根据课程id查询课程里面所有的小节
        LambdaQueryWrapper<EduVideo> videoQuery = new LambdaQueryWrapper();
        videoQuery.eq(EduVideo::getCourseId,courseId);
        List<EduVideo> videoList = videoService.list(videoQuery);
        //封装小节
        chapterVoList.forEach(chapterVo -> {

            List<VideoVo> videoVoList = videoList.stream()
                    .filter(video -> video.getChapterId().equals(chapterVo.getId()))
                    .map(video -> new VideoVo(video.getId(), video.getTitle(),video.getVideoSourceId()))
                    .collect(toList());

            chapterVo.setChildren(videoVoList);
        });
        return chapterVoList;
    }

    @Override
    public void deleteChapter(String chapterId) {
        // 根据chapterId 查询小节表 ，如果查询有数据，不进行删除。
        LambdaQueryWrapper<EduVideo> chapterQuery = new LambdaQueryWrapper<>();
        chapterQuery.eq(EduVideo::getChapterId,chapterId);
        int count = videoService.count(chapterQuery);
        if (count>0){
            throw GuliException.from("请先删除章节中的小节！");
        }
        boolean result = removeById(chapterId);
        if(!result){
            throw GuliException.from(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    public void deleteChapterByCourseId(String courseId) {
        LambdaQueryWrapper<EduChapter> chapterWrapper = new LambdaQueryWrapper<>();
        chapterWrapper.eq(EduChapter::getCourseId,courseId);
        boolean removeResult = this.remove(chapterWrapper);
        if(!removeResult){
            throw  GuliException.from(ResultCode.DELETE_ERROR);
        }
    }
}
